<template>
  <div class="subject">
    <span>{{ a }}</span>
    <span>{{ symbol }}</span>
    <span>{{ b }}</span>
    <span>=</span>
    <input type="number" v-model.number="num" />
    <button @click="submit">提交</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      num: null,
      status: "undo", // right  error  undo
    };
  },
  computed: {
    a() {
      return Math.floor(Math.random() * 11);
    },
    b() {
      return Math.floor(Math.random() * 11);
    },
    symbol() {
      const symbolArr = ["+", "-", "*", "%"];
      return symbolArr[Math.floor(Math.random() * 4)];
    },
  },
  methods: {
    submit(e) {
      /* if (this.num === null) {
        this.status = "undo";
        this.$emit("status-envent", this.status);
      } else if (this.num === this.a + this.b) {
        e.target.disabled = true;
        this.status = "right";
        this.$emit("status-envent", this.status);
      } else {
        e.target.disabled = true;
        this.status = "error";
        this.$emit("status-envent", this.status);
      } */
      if (this.num === null) {
        this.status = "undo";
        this.$emit("status-envent", this.status);
        return
      }
      switch (this.symbol) {
        case "+":
          this.num === this.a + this.b
            ? (this.status = "right")
            : (this.status = "error");
          e.target.disabled = true;
          this.$emit("status-envent", this.status);
          break;
        case "-":
          this.num === this.a - this.b
            ? (this.status = "right")
            : (this.status = "error");
          e.target.disabled = true;
          this.$emit("status-envent", this.status);
          break;
        case "*":
          this.num === this.a * this.b
            ? (this.status = "right")
            : (this.status = "error");
          e.target.disabled = true;
          this.$emit("status-envent", this.status);
          break;
        case "%":
          this.num === this.a % this.b
            ? (this.status = "right")
            : (this.status = "error");
          e.target.disabled = true;
          this.$emit("status-envent", this.status);
          break;
      }
    },
  },
};
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
.subject button {
  font-size: 16px;
  height: 26px;
  border: 1px solid #000;
  margin-left: 10px;
  background-color: #eee;
}
</style>
